import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:klcpopup/klcpopup.dart';
import '../enums/transgression_taxonomy.dart';

class TransgressionReportingPortal {
  static void manifestReportingRealm(
    BuildContext context, {
    String? targetId,
    String? targetType,
    Function(TransgressionTaxonomy)? onReportSubmitted,
  }) {
    final etherealPopupController = KLCPopupController();

    showKLCPopup(
      context,
      controller: etherealPopupController,
      verticalLayout: KLCPopupVerticalLayout.center,
      horizontalLayout: KLCPopupHorizontalLayout.center,
      showType: KLCPopupShowType.bounceIn,
      dismissType: KLCPopupDismissType.bounceOutToBottom,
      maskType: KLCPopupMaskType.dimmed,
      child: Material(
        color: Colors.transparent,
        child: _constructTransgressionSelectionRealm(
          etherealPopupController,
          targetId: targetId,
          targetType: targetType,
          onReportSubmitted: onReportSubmitted,
        ),
      ),
    );
  }

  /// 构建违规选择领域
  static Widget _constructTransgressionSelectionRealm(
    KLCPopupController controller, {
    String? targetId,
    String? targetType,
    Function(TransgressionTaxonomy)? onReportSubmitted,
  }) {
    return Container(
      margin: EdgeInsets.symmetric(horizontal: 30.w),
      decoration: BoxDecoration(
        color: const Color(0xFF2A2A2A),
        borderRadius: BorderRadius.circular(20.r),
        boxShadow: [
          BoxShadow(
            color: Colors.black.withValues(alpha: 0.3),
            blurRadius: 20,
            offset: const Offset(0, 10),
          ),
        ],
      ),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          // 标题区域
          Container(
            padding: EdgeInsets.all(20.w),
            decoration: BoxDecoration(
              color: const Color(0xFFCD8B76),
              borderRadius: BorderRadius.only(
                topLeft: Radius.circular(20.r),
                topRight: Radius.circular(20.r),
              ),
            ),
            child: Row(
              children: [
                Icon(Icons.report_problem, color: Colors.white, size: 24.w),
                SizedBox(width: 12.w),
                Expanded(
                  child: Text(
                    'Report Content',
                    style: TextStyle(
                      color: Colors.white,
                      fontSize: 18.sp,
                      fontWeight: FontWeight.bold,
                      fontFamily: 'Poppins',
                    ),
                  ),
                ),
                GestureDetector(
                  onTap: () => controller.dismiss(),
                  child: Icon(Icons.close, color: Colors.white, size: 24.w),
                ),
              ],
            ),
          ),
          // 选项列表
          Container(
            padding: EdgeInsets.all(16.w),
            child: Column(
              children:
                  TransgressionTaxonomy.values
                      .map(
                        (transgression) => _buildTransgressionOptionTile(
                          transgression,
                          controller,
                          targetId: targetId,
                          targetType: targetType,
                          onReportSubmitted: onReportSubmitted,
                        ),
                      )
                      .toList(),
            ),
          ),
        ],
      ),
    );
  }

  /// 构建违规选项瓦片
  static Widget _buildTransgressionOptionTile(
    TransgressionTaxonomy transgression,
    KLCPopupController controller, {
    String? targetId,
    String? targetType,
    Function(TransgressionTaxonomy)? onReportSubmitted,
  }) {
    final description = transgressionDescriptions[transgression] ?? '';

    return Container(
      margin: EdgeInsets.only(bottom: 8.h),
      child: Material(
        color: Colors.transparent,
        child: Builder(
          builder:
              (context) => InkWell(
                onTap:
                    () => _executeTransgressionReport(
                      context,
                      transgression,
                      controller,
                      targetId: targetId,
                      targetType: targetType,
                      onReportSubmitted: onReportSubmitted,
                    ),
                borderRadius: BorderRadius.circular(12.r),
                child: Container(
                  padding: EdgeInsets.symmetric(
                    horizontal: 16.w,
                    vertical: 12.h,
                  ),
                  decoration: BoxDecoration(
                    color: const Color(0xFF3A3A3A),
                    borderRadius: BorderRadius.circular(12.r),
                    border: Border.all(
                      color: Colors.white.withValues(alpha: 0.1),
                      width: 1,
                    ),
                  ),
                  child: Row(
                    children: [
                      Expanded(
                        child: Text(
                          description,
                          style: TextStyle(
                            color: Colors.white,
                            fontSize: 16.sp,
                            fontFamily: 'Poppins',
                          ),
                        ),
                      ),
                      Icon(
                        Icons.arrow_forward_ios,
                        color: Colors.white.withValues(alpha: 0.6),
                        size: 16.w,
                      ),
                    ],
                  ),
                ),
              ),
        ),
      ),
    );
  }

  /// 执行违规举报
  static void _executeTransgressionReport(
    BuildContext context,
    TransgressionTaxonomy transgression,
    KLCPopupController controller, {
    String? targetId,
    String? targetType,
    Function(TransgressionTaxonomy)? onReportSubmitted,
  }) {
    controller.dismiss();

    final description = transgressionDescriptions[transgression] ?? '';

    // 显示举报成功提示
    _showSuccessNotification(context, description);

    // 调用回调函数，传递举报类型
    onReportSubmitted?.call(transgression);
  }

  /// 显示成功通知
  static void _showSuccessNotification(
    BuildContext context,
    String description,
  ) {
    final successController = KLCPopupController();

    showKLCPopup(
      context,
      controller: successController,
      verticalLayout: KLCPopupVerticalLayout.center,
      horizontalLayout: KLCPopupHorizontalLayout.center,
      showType: KLCPopupShowType.bounceIn,
      dismissType: KLCPopupDismissType.bounceOutToBottom,
      maskType: KLCPopupMaskType.none,
      child: Material(
        color: Colors.transparent,
        child: Container(
          margin: EdgeInsets.symmetric(horizontal: 40.w),
          padding: EdgeInsets.all(20.w),
          decoration: BoxDecoration(
            color: const Color(0xFF4CAF50),
            borderRadius: BorderRadius.circular(16.r),
            boxShadow: [
              BoxShadow(
                color: Colors.black.withValues(alpha: 0.2),
                blurRadius: 10,
                offset: const Offset(0, 5),
              ),
            ],
          ),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              Icon(Icons.check_circle, color: Colors.white, size: 48.w),
              SizedBox(height: 16.h),
              Text(
                'Report Submitted',
                style: TextStyle(
                  color: Colors.white,
                  fontSize: 18.sp,
                  fontWeight: FontWeight.bold,
                  fontFamily: 'Poppins',
                ),
              ),
              SizedBox(height: 8.h),
              Text(
                'Thank you for reporting "$description". We will review this content.',
                textAlign: TextAlign.center,
                style: TextStyle(
                  color: Colors.white,
                  fontSize: 14.sp,
                  fontFamily: 'Poppins',
                ),
              ),
            ],
          ),
        ),
      ),
    );

    // 自动关闭成功提示
    Future.delayed(const Duration(seconds: 3), () {
      successController.dismiss();
    });
  }
}
